<template>
  <div class="flowAdd">
    <el-form label-position="left" ref="flowForm" :model="flow" label-width="80px" :rules="rules">
      <el-form-item label="审核流名" prop="name">
        <el-input v-model="flow.name"></el-input>
      </el-form-item>
      <el-form-item label="apiPath" prop="apiPath">
        <el-input v-model="flow.apiPath" placeholder="用于开启审批流，是流的唯一标识"></el-input>
      </el-form-item>
      <el-form-item prop="isUse" label="是否启用" label-width="200px">
        <el-switch v-model="flow.isUse" active-text="启用" inactive-text="禁用"></el-switch>
      </el-form-item>
      <el-form-item prop="isCrossNode" label="提交时是否支持跨节点" label-width="200px">
        <el-switch v-model="flow.isCrossNode" active-text="启用" inactive-text="禁用"></el-switch>
      </el-form-item>
      <el-form-item prop="isAppointMember" label="提交时是否支持指定审核人" label-width="200px">
        <el-switch v-model="flow.isAppointMember" active-text="启用" inactive-text="禁用"></el-switch>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" autosize v-model="flow.remark"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="save" type="primary">
          <span v-if="config.isChange">修改</span>
          <span v-else>添加</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "FlowAdd",
  props: ["isChange", "fFlow"],
  data() {
    return {
      flow: {
        id: null,
        name: "",
        apiPath: "",
        remark: "",
        isUse: true,
        isCrossNode: false,
        isAppointMember: false
      },
      config: {
        isChange: false
      },
      rules: {
        name: [{ required: true, message: "请输入组织名", trigger: "blur" }],
        apiPath: [
          { required: true, message: "apipath不能为空", trigger: "blur" }
        ],
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }]
      }
    };
  },
  watch: {
    isChange(val) {
      this.config.isChange = this.isChange;
    },
    fFlow(val) {
      if (this.fFlow == null) {
        this.flow = {
          id: null,
          name: "",
          apiPath: "",
          remark: "",
          isUse: true,
          isCrossNode: false,
          isAppointMember: false
        };
      } else {
        this.flow = JSON.parse(JSON.stringify(this.fFlow));
        this.flow.createAt = null;
      }
    }
  },
  created() {
    if (this.fFlow == null) {
      this.flow = {
        id: null,
        name: "",
        apiPath: "",
        remark: "",
        isUse: true,
        isCrossNode: false,
        isAppointMember: false
      };
    } else {
      this.flow = JSON.parse(JSON.stringify(this.fFlow));
      this.flow.createAt = null;
    }
    this.config.isChange = this.isChange;
  },
  methods: {
    save() {
      //调用服务
      this.$refs["flowForm"].validate(valid => {
        if (valid) {
          var that = this;
          this.$axios
            .get("flow/save", {
              params: this.flow
            })
            .then(res => {
              let data = res.data;
              if (data.code === 0) {
                this.$message({ message: data.msg, type: "success" });
                this.$emit("input", false);
                this.$emit("flush-data");
              } else {
                this.$message.error(data.msg);
              }
            })
            .catch(function(error) {
              console.log("axios请求异常:" + error);
              that.$message.error("网路请求异常");
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
</style>
